package net.trevize.galatee.main;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

import net.trevize.galatee.GEvent;
import net.trevize.galatee.GListener;
import net.trevize.galatee.Galatee;
import net.trevize.galatee.GalateeFactory;
import net.trevize.tinker.SystemCommandHandler2;

/**
 * 
 * 
 * @author Nicolas James <nicolas.james@gmail.com> [[http://njames.trevize.net]]
 * ImageNetPopulatedSynset.java - Dec 1, 2010
 */

public class ImageNetPopulatedSynset {

	private String path;

	public ImageNetPopulatedSynset(String path) {
		this.path = path;
		init();
	}

	private void init() {
		// setting the system look and feel
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}

		System.out.println("*** DirectoryExplorer with Galatee main test ***");
		System.out.println("initializing...");

		int n = 3; //initial number of columns.
		System.out.println("path=[" + path + "]");
		System.out.println("display in " + n + " columns");

		Galatee g = GalateeFactory.loadDatasetFromTAR(path);

		JFrame f = new JFrame("*** Galatee main test ***");

		JPanel p = new JPanel();
		p.setLayout(new BorderLayout());
		p.setBorder(new EmptyBorder(3, 3, 3, 3));
		f.getContentPane().add(p);
		p.add(g);

		GListener listener = new GListener() {

			class SystemCommandThread extends Thread {
				private SystemCommandHandler2 sch2 = new SystemCommandHandler2();
				private String command;

				public SystemCommandThread(String command) {
					this.command = command;
				}

				public void run() {
					String[] commands = new String[1];
					commands[0] = command;
					sch2.exec(commands);
				}
			}


			@Override
			public void itemDoubleClicked(GEvent e) {
				System.out.println("item doubleclicked ["
						+ e.getSelectedItem().getLocalFilepath() + "]");
				SystemCommandThread sct = new SystemCommandThread("display \""
						+ e.getSelectedItem().getLocalFilepath() + "\"");
				sct.start();
			}

			@Override
			public void selectionChanged(GEvent e) {
				System.out.println("changing selection for ["
						+ e.getSelectedItem().getLocalFilepath() + "]");
			}

		};
		g.addGalateeListener(listener);

		g.enableSearchFunctionality();

		System.out.println("initialization ended.");

		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setSize(512, 512);
		f.setLocationRelativeTo(null);
		f.setVisible(true);
	}

	public static void main(String args[]) {
		String archive_path = "/media/tambourine/ILSVRC2010/n01491361.tar";

		ImageNetPopulatedSynset explorer = new ImageNetPopulatedSynset(
				archive_path);
	}

}
